# encoding: UTF-8

def crc16A(dat):
    crc = 0xFFFF  # 65535
    index = 0
    for j in dat:
        crc = crc ^ ord(j)
        print("crc=%x" % crc)
        for i in range(8):
            if (crc & 1) == 1:
                crc = crc >> 1
                crc = crc ^ 0xA001  # 40961 多项式
            else:
                crc = crc >> 1
    return crc


if __name__ == '__main__':
    import sys
    import time
    import binascii
    import datetime
    from time import gmtime, strftime

    checks_dev2 = '010413870031'  # 01 04 13 87 00 31 85 73
    # 将字符串转成HEX数据
    # checks_dev2 = checks_dev2.replace(' ', '')
    checks_dev2bin = binascii.a2b_hex(checks_dev2)

    ret = crc16A(checks_dev2bin)
    print("hex=%x" % (ret))
    print(hex(binascii.crc32(checks_dev2bin)))
